<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Hello, World</title>
    <style type="text/css">
        html{height:100%}
        body{height:100%;margin:0px;padding:0px}
        #container{height:100%}
    </style>
    <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=20HO9VWIlnsREA1ikvhhFYNfj0HNixXv">
    </script>
</head>

<body>
<div id="container"></div>
<script type="text/javascript">
    var map = new BMap.Map("container");          // 创建地图实例
    var point = new BMap.Point(104.071, 30.576);  // 创建点坐标
    var myGeo = new BMap.Geocoder(); // 创建地理编码实例
    var addr = parent.document.getElementById('address').value;
    map.centerAndZoom(point, 15);                 // 初始化地图，设置中心点坐标和地图级别
    map.enableScrollWheelZoom(true);     //开启鼠标滚轮缩放

    if(addr){
        myGeo.getPoint(addr, function(p){
            if (p) {console.log(p)
                point = p;
                map.centerAndZoom(point, 16);
                init();
            }
        }, "成都市");
    } else {
        init();
    }

    function init(){
        var marker = new BMap.Marker(point);        // 创建标注
        map.addOverlay(marker);
        marker.enableDragging();
        marker.addEventListener("dragend", function(e){
            point = e.point;
            showMsg();
            console.log("当前位置：" + e.point.lng + ", " + e.point.lat);
        });
        marker.addEventListener("click", function(e){
            point = e.point;
            showMsg();
        });
    }


    function showMsg(){
        // 根据坐标得到地址描述
        myGeo.getLocation(point, function(result){
            if (result){console.log(result);
                var opts = {
                    width : 200,     // 信息窗口宽度
                    height: 100,     // 信息窗口高度
                    title : "" , // 信息窗口标题
                    enableMessage:true,//设置允许信息窗发送短息
                    message:"~"
                };
                var su = result.surroundingPois[0];
                var address = su ? su.address : result.address;
                var infoWindow = new BMap.InfoWindow("地址：" + address, opts);  // 创建信息窗口对象
                map.openInfoWindow(infoWindow,point); //开启信息窗口

                parent.document.getElementById('address').value = address;
            }
        });
    }
</script>
</body>
</html>